<?php
class Application_Model_CTDonDatHang extends Zend_Db_Table
{
	protected $_name='ct_dondathang';
	public function getCTDonDatHang($dondathang_id,$giohoa_id)
	{
		$dondathang_id = (int)$dondathang_id;
		$giohoa_id=(int)$giohoa_id;
		$row = $this->fetchRow('dondathang_id = ' . $dondathang_id.'and giohoa_id ='.$giohoa_id);
		if (!$row) {
			throw new Exception("Khong tim thay don dat hang");
		}
		return $row->toArray();
	}//phuong thuc tra ve mot dong theo $id

	public function getListCTDonDatHang($dondathang_id)
	{	
		try{
			$sql = 'Select * From `ct_dondathang` where dondathang_id = '.(int)$dondathang_id;
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_ASSOC);
			$list = $db->fetchAll ( $sql );
			return $list;
		}catch(Exception $ex){
			echo "Can not get gio hoa by id, Error: ".$ex->getMessage();
			exit;
		}
	}
	public function addCTDonDatHang($dondathang_id,$giohoa_id,$slmua)
	{
		try{
			$data = array(
					'dondathang_id'=>$dondathang_id,
					'giohoa_id'=>$giohoa_id,
					'slmua'=>$slmua,
			);
			//echo "<pre>";
			//print_r($data);
			//echo "</pre>";
			//die;
			$this->insert($data);
			//echo "insert thành công";die;
		}catch (Exception $ex){
			echo "Cannot add oderdetail . Error :".$ex->getMessage();
		}
	}//insert

	public function deleteCTDonDatHang($dondathang_id,$giohoa_id)
	{
		$this->delete('dondathang_id =' . (int)$dondathang_id.'and giohoa_id ='.$giohoa_id);
	}//drop
	
	public function getListGioHoa_bestselling(){
		try{
			$sql = 'SELECT giohoa_id
					FROM ct_dondathang
					GROUP BY giohoa_id
					ORDER BY SUM( slmua ) DESC 
					LIMIT 0 , 10';
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_ASSOC);
			$list = $db->fetchAll( $sql );
			return $list;
		}catch(Exception $ex){
			echo "Can not get list gio hoa id best selling, Error: ".$ex->getMessage();
			exit;
		}
	}
}